<template>
  <div class="params-define">
    <el-row type="flex" align="middle" style="padding: 5px 0px">
      <el-col :span="10">
        <div>定义外部参数</div>
      </el-col>
      <el-col :span="4">
        <el-button
          @click="dialogVisible = true"
          type="primary"
          icon="el-icon-edit"
          circle
        ></el-button>
      </el-col>
    </el-row>

    <el-dialog
      title="参数管理"
      destroy-on-close
      append-to-body
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :visible.sync="dialogVisible"
      width="60%"
    >
      <el-button @click="innerVisible = true">添加参数</el-button>
      <el-table :data="store.paramsDefine" style="width: 100%">
        <el-table-column prop="field" label="字段名称"> </el-table-column>
        <el-table-column prop="desc" label="参数注释"> </el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <el-button
              @click.native.prevent="
                editorRow(scope.$index, store.paramsDefine)
              "
              type="text"
              size="small"
            >
              编辑
            </el-button>
            <el-button
              @click.native.prevent="
                deleteRow(scope.$index, store.paramsDefine)
              "
              type="text"
              size="small"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-dialog
        width="60%"
        destroy-on-close
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="innerVisible"
        append-to-body
      >
        <el-form
          :model="form"
          :label-position="'left'"
          ref="form"
          label-width="100px"
          class="demo-form"
        >
          <el-form-item
            label="参数字段"
            prop="field"
            :rules="[
              { required: true, message: '请输入字段名', trigger: 'blur' },
            ]"
          >
            <el-input v-model="form.field"></el-input>
          </el-form-item>
          <el-form-item
            label="参数描述"
            prop="desc"
            :rules="[
              { required: true, message: '请输入参数描述', trigger: 'blur' },
            ]"
          >
            <el-input v-model="form.desc"></el-input>
          </el-form-item>
        </el-form>

        <span slot="footer" class="dialog-footer">
          <el-button @click="innerVisible = false">取 消</el-button>
          <el-button type="primary" @click="addEvent('form')">确 定</el-button>
        </span>
      </el-dialog>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props: ["store"],
  data() {
    return {
      dialogVisible: false,
      innerVisible: false,
      form: {
        field: "",
        desc: "",
        keyCode: "",
      },
    };
  },
  methods: {
    random() {
      let len = 8;
      let $chars = "ABCDEFGHJKMNPQRSTWXYZ";
      let maxPos = $chars.length;
      let id = "";
      for (let i = 0; i < len; i++) {
        id += $chars.charAt(Math.floor(Math.random() * maxPos));
      }
      return id;
    },
    addEvent(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let keyCode = null;
          if (!this.form.keyCode) {
            keyCode = this.random();
            this.store.paramsDefine.push({
              value: keyCode,
              field: this.form.field,
              desc: this.form.desc,
            });
          } else {
            keyCode = this.form.keyCode;
            let findEvent = this.store.paramsDefine.find((item) => {
              return item.value == keyCode;
            });
            Object.assign(findEvent, {
              value: keyCode,
              field: this.form.field,
              desc: this.form.desc,
            });
          }
          this.innerVisible = false;
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    editorRow(index, rows) {
      this.innerVisible = true;
      this.form.field = rows[index]["field"];
      this.form.keyCode = rows[index]["value"];
      this.form.desc = rows[index]["desc"];
    },
    deleteRow(index, rows) {
      delete this.store.eventMap[rows[index].value];
      rows.splice(index, 1);
    },
  },
  watch: {
    innerVisible(value) {
      if (!value) {
        this.$refs["form"].resetFields();
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.form-config,
.box-model-config {
  padding-left: 10px;
}
.form-config .title,
.box-model-config .title {
  line-height: 32px;
  font-size: 14px;
}
.form-config .el-input-number,
.box-model-config .el-input-number {
  line-height: 30px;
}
</style>